package com.charlotte.client;

import com.charlotte.bean.URLShortenerExpandBean;
import com.charlotte.bean.URLShortenerShortBean;
import com.google.gson.Gson;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

/**
 *
 * @author law
 */
public class URLShortenerClient 
{
 
    private final String URL = "https://www.googleapis.com/urlshortener/v1/url";
    private final Gson gson = new Gson();
    
    public URLShortenerClient()
    {}
    
    public URLShortenerShortBean createShortURL(String longUrl) throws MalformedURLException, IOException,
            java.net.SocketTimeoutException, NullPointerException
    {
        if(longUrl != null ? longUrl.isEmpty() : true)
            throw new NullPointerException("Debe definir una url para shortenear xD...u_U");
        
        HttpURLConnection conect = (HttpURLConnection)new URL(URL).openConnection();
        conect.setRequestMethod("POST");
        conect.setDoInput(true);
        conect.setDoOutput(true);
        conect.setRequestProperty("Content-Type","application/json");
        conect.setRequestProperty("Accept","application/json");
        conect.setReadTimeout(20000);
        conect.setConnectTimeout(20000);
        conect.connect();
        try(BufferedWriter out = new BufferedWriter(new OutputStreamWriter(conect.getOutputStream())))
        {
            out.write(("{\"longUrl\":\""+longUrl+"\"}").toCharArray());
        }
        StringWriter string = new StringWriter();
        try(BufferedReader out = new BufferedReader(new InputStreamReader(conect.getInputStream())))
        {
            readStream(string,out);
        }
        conect.disconnect();
        return gson.<URLShortenerShortResult>fromJson(new String(string.getBuffer()),URLShortenerShortResult.class);
    }
    
    private void readStream(StringWriter string, BufferedReader reader) throws IOException
    {
        char[] buffer = new char[5];
        int lectura;
        while((lectura = reader.read(buffer)) != -1)
        {
            string.write(buffer,0,lectura);
            string.flush();
        }
    }
    
    public URLShortenerExpandBean createLongURL(String shortUrl) throws MalformedURLException, IOException,
            java.net.SocketTimeoutException, NullPointerException
    {
        if(shortUrl != null ? shortUrl.isEmpty() : true)
            throw new NullPointerException("Debe definir una url para longtenear xD...u_U");
        
        if(shortUrl.matches("\u0020"))
            shortUrl = shortUrl.replaceAll(" ","%20");
        
        HttpURLConnection conect = (HttpURLConnection)new URL(URL+"?shortUrl="+shortUrl).openConnection();
        conect.setRequestMethod("GET");
        conect.setDoInput(true);
        conect.setDoOutput(true);
        conect.setRequestProperty("Content-Type","text/plain");
        conect.setRequestProperty("Accept","application/json");
        conect.setReadTimeout(20000);
        conect.setConnectTimeout(20000);
        conect.connect();
        StringWriter string = new StringWriter();
        try(BufferedReader out = new BufferedReader(new InputStreamReader(conect.getInputStream())))
        {
            readStream(string,out);
        }
        conect.disconnect();
        return gson.<URLShortenerExpandResult>fromJson(new String(string.getBuffer()),URLShortenerExpandResult.class);
    }
    
    private class URLShortenerExpandResult extends URLShortenerShortResult implements URLShortenerExpandBean
    {

        private String status;
        
        public URLShortenerExpandResult()
        {
            super();
            status = null;
        }
        
        public void setStatus(String status)
        {
            this.status = status;
        }
        
        @Override
        public String getStatus()
        {
            return status;
        }
        
    }
    
    private class URLShortenerShortResult implements URLShortenerShortBean
    {
        
        private String kind;
        private String id;
        private String longUrl;
        
        public URLShortenerShortResult()
        {
            kind = null;
            id = null;
            longUrl = null;
        }

        public void setKind(String kind) 
        {
            this.kind = kind;
        }

        public void setId(String id) 
        {
            this.id = id;
        }

        public void setLongUrl(String longUrl)
        {
            this.longUrl = longUrl;
        }

        @Override
        public String getKind() 
        {
            return kind;
        }

        @Override
        public String getId() 
        {
            return id;
        }

        @Override
        public String getLongUrl() 
        {
            return longUrl;
        }
        
    }
    
}